package com.lry.web.admin.controller;

import com.lry.web.cartoon.controller.CartoonController;
import com.lry.web.cartoon.entity.Cartoon;
import com.lry.web.cartoon.entity.CartoonVariable;
import com.lry.web.cartoon.service.CartoonService;
import com.lry.web.cartoon.service.CartoonUpdateService;
import com.lry.web.cartoon.service.CartoonVariableService;
import com.lry.web.fiction.controller.FictionController;
import com.lry.web.fiction.entity.Fiction;
import com.lry.web.fiction.entity.FictionVariable;
import com.lry.web.fiction.service.FictionService;
import com.lry.web.fiction.service.FictionUpdateService;
import com.lry.web.fiction.service.FictionVariableService;
import com.lry.web.information.service.InformationService;
import com.lry.web.listener.SurviceListener;
import com.lry.web.user.entity.User;
import com.lry.web.util.TransformUtil;
import com.lry.web.video.controller.VideoController;
import com.lry.web.video.entity.Video;
import com.lry.web.video.entity.VideoVariable;
import com.lry.web.video.service.VideoService;
import com.lry.web.video.service.VideoUpdateService;
import com.lry.web.video.service.VideoVariableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    FictionService fictionService;
    @Autowired
    FictionVariableService fictionVariableService;
    @Autowired
    VideoService videoService;
    @Autowired
    VideoVariableService videoVariableService;
    @Autowired
    CartoonService cartoonService;
    @Autowired
    CartoonVariableService cartoonVariableService;
    @Autowired
    InformationService informationService;
    @Autowired
    VideoUpdateService videoUpdateService;
    @Autowired
    FictionUpdateService fictionUpdateService;
    @Autowired
    CartoonUpdateService cartoonUpdateService;

    //首页
    @RequestMapping("/admin/index")
    public String index(){
        User user = (User)request.getSession().getAttribute("user");
        request.setAttribute("user",user);
        return "index";
    }
    //登录页面
    @RequestMapping("/admin/login")
    public String login(){
        return "login";
    }

    //小说列表
    @RequestMapping(value = {"/admin/fiction_list/{from}/{size}"})
    public String fictionList(@PathVariable(value = "from") Integer from ,@PathVariable(value = "size") Integer size,@RequestParam(value = "option",required=false)String option,@RequestParam(value = "key",required=false) String key ){
        String optionn = null;
        if(option==""||option==null){
            option = null;
        }else{
            optionn =TransformUtil.camelToUnderline(option);
        }
        if(key==""||key==null){
            key = null;
            option = null;
        }else{
            optionn =TransformUtil.camelToUnderline(option);
        }
        Map<String,Object> map = fictionService.selectFictions(from,size,optionn,key);
        request.setAttribute("fictions",map.get("fictions"));
        request.setAttribute("pages",map.get("pages"));
        request.setAttribute("from",map.get(from));
        request.setAttribute("fictionUp",fictionUpdateService.selectByFiction((List<Fiction>)map.get("fictions")));
        request.setAttribute("option",option);
        request.setAttribute("key",key);
        request.setAttribute("istatus", FictionController.istatus);
        return "fiction_list";
    }
    //小说规则添加
    @RequestMapping(value = {"/admin/fiction_variable_add/{id}","/admin/fiction_variable_add"})
    public String fictionVariableAdd(@PathVariable(value = "id", required = false) Integer id){
        if(id!=null){
            FictionVariable fv =  fictionVariableService.getFictionVariable(id);
            request.setAttribute("fv",fv);
        }else{
            request.setAttribute("fv",new FictionVariable());
        }
        return "fiction_variable_add";
    }

    //小说规则列表
    @RequestMapping(value = {"/admin/fiction_variable_list/{from}/{size}"})
    public String fictionVariableList(@PathVariable(value = "from") Integer from ,@PathVariable(value = "size") Integer size){
        Map<String,Object> map = fictionVariableService.selectFictionVariables(from,size);
        request.setAttribute("fictionVariables",map.get("fictionVariables"));
        request.setAttribute("pages",map.get("pages"));
        request.setAttribute("from",map.get(from));
        return "fiction_variable_list";
    }


    //漫画列表
    @RequestMapping(value = {"/admin/cartoon_list/{from}/{size}"})
    public String cartoonList(@PathVariable(value = "from") Integer from ,@PathVariable(value = "size") Integer size,@RequestParam(value = "option",required=false)String option,@RequestParam(value = "key",required=false) String key ){
        String optionn = null;
        if(option==""||option==null){
            option = null;
        }else{
            optionn =TransformUtil.camelToUnderline(option);
        }
        if(key==""||key==null){
            key = null;
            option = null;
        }else{
            optionn =TransformUtil.camelToUnderline(option);
        }
        Map<String,Object> map = cartoonService.selectCartoons(from,size,optionn,key);
        request.setAttribute("cartoons",map.get("cartoons"));
        request.setAttribute("pages",map.get("pages"));
        request.setAttribute("from",map.get(from));
        request.setAttribute("cartoonUp",cartoonUpdateService.selectByCartoon((List<Cartoon>)map.get("cartoons")));
        request.setAttribute("option", option);
        request.setAttribute("key",key);
        request.setAttribute("istatus", CartoonController.istatus);
        return "cartoon_list";
    }
    //漫画规则添加
    @RequestMapping(value = {"/admin/cartoon_variable_add/{id}","/admin/cartoon_variable_add"})
    public String cartoonVariableAdd(@PathVariable(value = "id", required = false) Integer id){
        if(id!=null){
            CartoonVariable cv =  cartoonVariableService.getCartoonVariable(id);
            request.setAttribute("cv",cv);
        }else{
            request.setAttribute("cv",new CartoonVariable());
        }
        return "cartoon_variable_add";
    }
    //漫画规则列表
    @RequestMapping(value = {"/admin/cartoon_variable_list/{from}/{size}"})
    public String cartoonVariableList(@PathVariable(value = "from") Integer from ,@PathVariable(value = "size") Integer size){
        Map<String,Object> map = cartoonVariableService.selectCartoonVariables(from,size);
        request.setAttribute("cartoonVariables",map.get("cartoonVariables"));
        request.setAttribute("pages",map.get("pages"));
        request.setAttribute("from",map.get(from));
        return "cartoon_variable_list";
    }


    //视频列表
    @RequestMapping(value = {"/admin/video_list/{from}/{size}"})
    public String videoList(@PathVariable(value = "from") Integer from ,@PathVariable(value = "size") Integer size,@RequestParam(value = "option",required=false)String option,@RequestParam(value = "key",required=false) String key ){
        String optionn = null;
        if(option==""||option==null){
            option = null;
        }else{
            optionn =TransformUtil.camelToUnderline(option);
        }
        if(key==""||key==null){
            key = null;
            option = null;
        }else{
            optionn =TransformUtil.camelToUnderline(option);
        }
        Map<String,Object> map = videoService.selectVideos(from,size,optionn,key);
        request.setAttribute("videos",map.get("videos"));
        request.setAttribute("pages",map.get("pages"));
        request.setAttribute("from",map.get(from));
        request.setAttribute("videoUp",videoUpdateService.selectByVideo((List<Video>)map.get("videos")));
        request.setAttribute("option",option);
        request.setAttribute("key",key);
        request.setAttribute("istatus", VideoController.istatus);
        return "video_list";
    }
    //视频规则添加
    @RequestMapping(value = {"/admin/video_variable_add/{id}","/admin/video_variable_add"})
    public String videoVariableAdd(@PathVariable(value = "id", required = false) Integer id){
        if(id!=null){
            VideoVariable vv =  videoVariableService.getVideoVariable(id);
            request.setAttribute("vv",vv);
        }else{
            request.setAttribute("vv",new VideoVariable());
        }
        return "video_variable_add";
    }
    //视频规则列表
    @RequestMapping(value = {"/admin/video_variable_list/{from}/{size}"})
    public String videoVariableList(@PathVariable(value = "from") Integer from ,@PathVariable(value = "size") Integer size){
        Map<String,Object> map = videoVariableService.selectVideoVariables(from,size);
        request.setAttribute("videoVariables",map.get("videoVariables"));
        request.setAttribute("pages",map.get("pages"));
        request.setAttribute("from",map.get(from));
        return "video_variable_list";
    }




    @RequestMapping("/admin/error")
    public String error(){
        return "error";
    }

    @RequestMapping("/admin/main")
    public String main(){
        request.setAttribute("nodeNum", SurviceListener.uuidSet.size());
        request.setAttribute("fictionNum",fictionService.selectNum());
        request.setAttribute("cartoonNum",cartoonService.selectNum());
        request.setAttribute("videoNum",videoService.selectNum());
        request.setAttribute("informations",informationService.selectInformations());
        return "main";
    }

    @RequestMapping({"/index","/"})
    public String home(){
        return "home";
    }

}
